<?php

namespace app\api\controller;

use addons\epay\library\Service;
use app\common\controller\Api;
use app\common\model\Payment;
use app\common\model\Recharge;
use app\common\model\RechargeLog;

/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];


    /**
     * 支付方式
     */
    public function payment(){
        $list = Payment::where('status','normal')->paginate();

        $this->success(__('Get success'),$list);
    }

    /**
     * 支付配置
     */
    public function recharge(){
        $list = Recharge::where('status','normal')->paginate();

        $this->success(__('Get success'),$list);
    }

    public function image(){

// 要添加水印的图片路径
        $sourceFile = ROOT_PATH.'public/uploads/60665900b6224f308abd93e838dd444f.png';
        $imagePath = ROOT_PATH.'public/uploads/60665900b6224f308abd93e838dd444f.png';



        $font = ROOT_PATH.'public/uploads/StdNW8.otf';; // 指定一个字体文件
        $fontPath = ROOT_PATH.'public/uploads/StdNW8.otf';; // 指定一个字体文件

        $text = '朝日と闇が入り交じる,この世で最も強烈な愛と憎しみがここに現れます 2171734506';
        $fontSize = 20;

//        $image = \think\Image::open($sourceFile);
// 给原图左上角添加水印并保存water_image.png
//        $image->text($text,$font,60,'#666666')->save('text_image.png');
//
        $image = \think\Image::open($imagePath);
        $width = $image->width();
        $height = $image->height();

        $image->tiletext($text,$font,15,'#999999',0,$width/3.5,30)->save('text_image.png');
        die;


        $res = filetype($sourceFile);

// 创建图片对象
        $image = imagecreatefrompng($sourceFile);

// 指定水印文字和字体


// 分配颜色和透明度
        $color = imagecolorallocatealpha($image, 255, 255, 255, 50); // 白色透明度50

// 设置文字位置和旋转角度
        $x = 100; // 水平位置
        $y = 100; // 垂直位置
        $angle = 0; // 旋转角度

        // 将文字添加到图片上
        imagettftext($image, 20, $angle, $x, $y, $color, $font, $text);

// 输出图片
        header('Content-Type: image/jpeg');
        imagepng($image);

// 释放内存
        imagedestroy($image);

    }

    /**
     * 创建支付订单
     */
    public function paycreate(){
        $amount = $this->request->post('amount');

        $type = $this->request->post('type','alipay');

        if(!$amount){
            $this->error(__('Invalid parameters'));
        }

        //创建一个支付订单
        $data = [
            'money'     => $amount,
            'user_id'   => $this->auth->id,
            'order_sn'  => "ND".date("YmdHis") . mt_rand(100000, 999999),
            'paytype'   => $type,
        ];
        $order  = RechargeLog::create($data);

        $this->success(__('Get success'),$order);
    }

    public function pay(){

        $id = $this->request->get('id');
        if(!$id){
            $this->error(__('Invalid parameters'));
        }
        $openid = $this->request->post();
        //订单号
        $order = RechargeLog::get($id);
        $out_trade_no = $order['order_sn'];
        $type = $order['paytype'];
        $amount = $order['money'];
        if($type == 'alipay'){
            $method = 'wap';
        }else{
            $method = 'wap';

        }

        //订单标题
        $title = '虚拟订单：'.$out_trade_no;

        //回调链接
        $notifyurl = $this->request->root(true) . '/addons/epay/index/notifyx/paytype/' . $type;
        $returnurl = $this->request->root(true) . '/addons/epay/index/returnx/paytype/' . $type . '/out_trade_no/' . $out_trade_no;
        $response = Service::submitOrder($amount, $out_trade_no, $type, $title, $notifyurl, $returnurl, $method, $openid);

        return $response->getContent();
    }
}
